package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Forum;
import com.skyolder.milkbee.service.ForumService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Forum.
 */
@RestController
@RequestMapping("/api")
public class ForumResource {

	private final Logger log = LoggerFactory.getLogger(ForumResource.class);

	@Inject
	private ForumService forumService;

	/**
	 * POST /forums : Create a new forum.
	 *
	 * @param forum
	 *            the forum to create
	 * @return the ResponseEntity with status 201 (Created) and with body the
	 *         new forum, or with status 400 (Bad Request) if the forum has
	 *         already an ID
	 * @throws URISyntaxException
	 *             if the Location URI syntax is incorrect
	 */
	@PostMapping("/forums")
	@Timed
	public ResponseEntity<Forum> createForum(@RequestBody Forum forum) throws URISyntaxException {
		log.debug("REST request to save Forum : {}", forum);
		if (forum.getId() != null) {
			return ResponseEntity.badRequest()
					.headers(
							HeaderUtil.createFailureAlert("forum", "idexists", "A new forum cannot already have an ID"))
					.body(null);
		}
		return ResponseEntity.badRequest()
				.headers(HeaderUtil.createFailureAlert("forum", "fail", "can not create forum")).body(null);
		/*
		 * Forum result = forumService.save(forum); return
		 * ResponseEntity.created(new URI("/api/forums/" + result.getId()))
		 * .headers(HeaderUtil.createEntityCreationAlert("forum",
		 * result.getId().toString())) .body(result);
		 */
	}

	/**
	 * PUT /forums : Updates an existing forum.
	 *
	 * @param forum
	 *            the forum to update
	 * @return the ResponseEntity with status 200 (OK) and with body the updated
	 *         forum, or with status 400 (Bad Request) if the forum is not
	 *         valid, or with status 500 (Internal Server Error) if the forum
	 *         couldnt be updated
	 * @throws URISyntaxException
	 *             if the Location URI syntax is incorrect
	 */
	@PutMapping("/forums")
	@Timed
	public ResponseEntity<Forum> updateForum(@RequestBody Forum forum) throws URISyntaxException {
		log.debug("REST request to update Forum : {}", forum);
		if (forum.getId() == null) {
			return createForum(forum);
		}
		Forum result = forumService.save(forum);
		return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert("forum", forum.getId().toString()))
				.body(result);
	}

	/**
	 * GET /forums : get all the forums.
	 *
	 * @param pageable
	 *            the pagination information
	 * @return the ResponseEntity with status 200 (OK) and the list of forums in
	 *         body
	 * @throws URISyntaxException
	 *             if there is an error to generate the pagination HTTP headers
	 */
	@GetMapping("/forums")
	@Timed
	public ResponseEntity<List<Forum>> getAllForums(@ApiParam Pageable pageable) throws URISyntaxException {
		log.debug("REST request to get a page of Forums");
		Page<Forum> page = forumService.findAll(pageable);
		if(page.getContent()==null||page.getContent().size()<=0){
			Forum forum=Forum.defaultNewInstance();
			forum.setId(1L);
			forumService.save(forum);
			page = forumService.findAll(pageable);
		}
		HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/forums");
		return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
	}

	/**
	 * GET /forums/:id : get the "id" forum.
	 *
	 * @param id
	 *            the id of the forum to retrieve
	 * @return the ResponseEntity with status 200 (OK) and with body the forum,
	 *         or with status 404 (Not Found)
	 */
	@GetMapping("/forums/{id}")
	@Timed
	public ResponseEntity<Forum> getForum(@PathVariable Long id) {
		log.debug("REST request to get Forum : {}", id);
		Forum forum = forumService.findOne(id);
		return Optional.ofNullable(forum).map(result -> new ResponseEntity<>(result, HttpStatus.OK))
				.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
	}

	/**
	 * DELETE /forums/:id : delete the "id" forum.
	 *
	 * @param id
	 *            the id of the forum to delete
	 * @return the ResponseEntity with status 200 (OK)
	 */
	@DeleteMapping("/forums/{id}")
	@Timed
	public ResponseEntity<Void> deleteForum(@PathVariable Long id) {
		log.debug("REST request to delete Forum : {}", id);
		return ResponseEntity.badRequest()
				.headers(HeaderUtil.createFailureAlert("forum", "fail", "can not delete forum")).body(null);
		/*
		 * forumService.delete(id); return
		 * ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(
		 * "forum", id.toString())).build();
		 */
	}

}
